package game.seungha;

import java.util.Random;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

public class Adapter_NumberGrid extends BaseAdapter
{
	Activity_Play 	mActivity;
	Button 		mButton[];
	
	int			mCount;
	boolean		isSet_rndNumber[];
	
	Adapter_NumberGrid(Activity_Play activity)
	{
		mActivity 	= activity;
		this.setting_NumberButton();
	}
	
	void setting_NumberButton()
	{
		int 	rndNumber;
		int 	rndIndex;
		Random 	rnd;
		
		//Custom Class
		NGInfo 				gameInfo;
		Listener_NumberButton	listener_NumberButton;
		
		
		rnd = new Random(System.currentTimeMillis());
		
		listener_NumberButton	= new Listener_NumberButton(mActivity);
		gameInfo 				= NGInfo.getInstance();
		
		mCount = (int) Math.pow((gameInfo.getLevel()+2), 2);
		Log.i("DBG", "[Adapter_NumberGrid]Adapter_NumberGrid"+" mCount:"+mCount);

		mButton 		= new Button[mCount];
		isSet_rndNumber	= new boolean[mCount];
		
		for(int i=0; i<mButton.length; i++)
		{
			mButton[i] = new Button(mActivity);
			
			while(true)
			{
				rndNumber 	= rnd.nextInt(mCount)+1; // Random Number Create 0~
				rndIndex 	= rndNumber-1;
				
				if(isSet_rndNumber[rndIndex] == false)
				{
					Log.i("DBG", "[Adapter_NumberGrid]setting_NumberButton"+" i:"+i+" rndNumber:"+rndNumber);
					
					mButton[i].setText(String.valueOf(rndNumber));
					mButton[i].setTextSize(NG.TextSize.NUMBER_BUTTON);

					mButton[i].setTag(R.id.TAG_BUTTON_NUMBER, rndNumber);
					mButton[i].setTag(R.id.TAG_TEXT_SHOW,  	  true);
					
					isSet_rndNumber[rndIndex] = true;
					break;
				}
			}//while(true)
			
			mButton[i].setOnClickListener(listener_NumberButton);
			
		}
		
		listener_NumberButton.setButtonArray(mButton);
		Log.i("DBG", "[Adapter_NumberGrid]setting_NumberButton");
		
	}
	
	
	@Override
	public int getCount() 
	{
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) 
	{
		if(position < 0)return null;
		if(position > mCount)return null;

		return mButton[position];
	}

	@Override
	public long getItemId(int position) 
	{
		if(position < 0)return -1;
		if(position > mCount)return -1;

		return mButton[position].getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		
		if(position < 0)return null;
		if(position > mCount)return null;

		return mButton[position];
	}
	

}























